//
//  IAADetailRootVC.m
//  iPadAuditAuthorise
//
//  Created by XiangDongMBP on 16/9/7.
//  Copyright © 2016年 XiangDong. All rights reserved.
//

#import "IAADetailRootVC.h"
#import "IAAMarketDetailVC.h"
#import "IAARiskMonitorInfoVC.h"
#import "IAARiskMonitorAlarmVC.h"
#import "IAAAuthCheckDetailVC.h"
#import <Masonry.h>
#import "GlobalHeader.h"
#import "IAAAuthCheckModel.h"

@interface IAADetailRootVC ()

@property (weak, nonatomic) IBOutlet UIView *leftBgView;
@property (weak, nonatomic) IBOutlet UIButton *marketBtn;
@property (weak, nonatomic) IBOutlet UIButton *riskInfoBtn;
@property (weak, nonatomic) IBOutlet UIButton *riskAlarmBtn;
@property (weak, nonatomic) IBOutlet UIButton *authDetailBtn;

@property (strong, nonatomic) UIPageViewController *pageVC;
@property (strong, nonatomic) NSArray<UIViewController *> *vcArray;
@property (strong, nonatomic) NSArray<UIButton *> *btnArray;
@property (assign, nonatomic) NSInteger index;

@end

@implementation IAADetailRootVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //设置导航栏
    [self initNavigationBar];
    //設置子Views
    [self initSubViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    NSLog(@"%s", __FUNCTION__);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initNavigationBar {
    self.navigationItem.title = @"详情界面";
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemTapped:)];
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)initSubViews {    
    [self.marketBtn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.riskInfoBtn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.riskAlarmBtn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.authDetailBtn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    IAAMarketDetailVC * vc1 = [[IAAMarketDetailVC alloc] init];
    vc1.busiId = self.taskItem.busiId;
    
    IAARiskMonitorInfoVC * vc2 = [[IAARiskMonitorInfoVC alloc] initWithType:IAARiskMonitorTypeInfo];
    vc2.busiId = self.taskItem.busiId;
    
//    IAARiskMonitorAlarmVC * vc3 = [[IAARiskMonitorAlarmVC alloc] init];
    IAARiskMonitorInfoVC * vc3 = [[IAARiskMonitorInfoVC alloc] initWithType:IAARiskMonitorTypeAlarm];
    vc3.busiId = self.taskItem.busiId;
    
    IAAAuthCheckDetailVC * vc4 = [[IAAAuthCheckDetailVC alloc] init];
    vc4.taskItem = self.taskItem;
    vc4.model = self.authDetailModel;
    
    self.vcArray = @[vc1, vc2, vc3, vc4];
    
    self.btnArray = @[self.marketBtn, self.riskInfoBtn, self.riskAlarmBtn, self.authDetailBtn];
    
    self.pageVC = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationVertical options:nil];
    [self.pageVC setViewControllers:@[vc4] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    self.navigationItem.title = vc4.navigationItem.title;
    self.authDetailBtn.selected = YES;
    
    [self addChildViewController:self.pageVC];
    [self.view addSubview:self.pageVC.view];
    [self.pageVC didMoveToParentViewController:self];
    
    // 添加布局约束
    //    UIView * superView = self.view;
    [self.pageVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(0);
        make.leading.equalTo(self.leftBgView.mas_trailing).with.offset(0);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(0);
        make.trailing.equalTo(self.view.mas_trailing).with.offset(0);
//        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(64, 100, 0, -100));
    }];
}

- (BOOL)hidesBottomBarWhenPushed {
    return YES;
}

#pragma mark - 按键处理
- (void)btnTapped:(UIButton *)btn {
    self.index = [self.btnArray indexOfObject:btn];
    [self selectBtnAtIndex:self.index];
    
    UIViewController *vc = self.vcArray[self.index];
    [self.pageVC setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    self.navigationItem.title = vc.navigationItem.title;
}

- (void)selectBtnAtIndex:(NSInteger)index {
    [self.btnArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = (index == idx);
    }];
}

- (void)leftItemTapped:(id)sender {
    IAAAuthCheckDetailVC *vc4 = (IAAAuthCheckDetailVC *)self.vcArray.lastObject;
    [vc4 askToAbandonTask];
}

@end
